package com.hbue.water.controller;

import com.github.pagehelper.PageInfo;
import com.hbue.water.common.CommonResult;
import com.hbue.water.common.ResponseCode;
import com.hbue.water.entity.Customer;
import com.hbue.water.entity.History;
import com.hbue.water.entity.HistoryContainer;
import com.hbue.water.entity.Worker;
import com.hbue.water.service.CustomerService;
import com.hbue.water.service.HistoryContainerService;
import com.hbue.water.service.HistoryService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yushu
 * @date 2021/12/27
 * @apiNate
 */
@RestController
@RequestMapping("/history")
@Slf4j
@ApiModel("送水历史Controller")
public class HistoryController {

    @Autowired
    private HistoryService historyService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private HistoryContainerService historyContainerService;

    @ApiOperation("分页查询所有送水历史信息")
    @GetMapping("/listHistoryPage/{pageNum}/{pageSize}")
    public CommonResult<PageInfo> listHistory(
            @ApiParam(name = "pageNum", value = "当前页", required = true)
            @PathVariable("pageNum") Integer pageNum,
            @ApiParam(name = "pageSize", value = "每页数据量", required = true)
            @PathVariable("pageSize") Integer pageSize) {
        PageInfo<History> historyPageInfo = historyService.listHistory(pageNum, pageSize);
        return new CommonResult<>(ResponseCode.RESPONSE_SUCCESS, "success", historyPageInfo);
    }

    @ApiOperation("展示所有送水历史信息")
    @GetMapping("/listHis")
    public CommonResult<List<History>> listHistory() {
        List<History> hisList = historyService.listHistory();
        return new CommonResult<>(ResponseCode.RESPONSE_SUCCESS, "ok", hisList);
    }

    @ApiOperation("点击添加按钮，查询客户列表和送水工列表，渲染到前端")
    @GetMapping("/preSaveHis")
    public CommonResult<HistoryContainer> preSaveHistory() {
        HistoryContainer historyContainer = historyContainerService.getWorkerListAndCustomerList();
        return new CommonResult<>(ResponseCode.RESPONSE_SUCCESS, "ok", historyContainer);
    }

    @ApiOperation("添加送水历史")
    @PostMapping("/saveHis")
    public CommonResult<Integer> saveHistory(
            @ApiParam(name = "history", value = "前端采集的送水历史信息", required = true)
            @RequestBody History history) {
        log.info("History history = " + history);
        Customer customer = customerService.getById(history.getCustomer().getCid());
        if (history.getSendWaterCount() > customer.getCustTicket()) {
            return new CommonResult<>(ResponseCode.RESPONSE_FAIL, "fail");
        } else {
            // 将送水工编号和客户编号注入到History对象
            int rows = historyService.saveHistory(history);
            return new CommonResult<>(ResponseCode.RESPONSE_SUCCESS, "ok", rows);
        }
    }

    @ApiOperation("根据id查询送水历史信息，数据回显")
    @GetMapping("/getHisById/{hid}")
    public CommonResult<History> getHistoryById(
            @ApiParam(name = "hid", value = "送水历史id", required = true)
            @PathVariable("hid") Integer hid) {
        History history = historyService.getHistoryById(hid);
        if (null == history) {
            return new CommonResult<>(ResponseCode.RESPONSE_FAIL, "null");
        } else {
            return new CommonResult<>(ResponseCode.RESPONSE_SUCCESS, "ok", history);
        }
    }

    @ApiOperation("根据客户id查询是否还有该记录")
    @GetMapping("/getHistoryCountByCustId/{cid}")
    public CommonResult<Integer> getHistoryCountByCustId(
            @ApiParam(name = "cid", value = "客户id", required = true)
            @PathVariable("cid") Integer cid) {
        Integer count = historyService.getCountById(cid);
        return new CommonResult<>(ResponseCode.RESPONSE_SUCCESS, "ok", count);
    }

    @ApiOperation("修改送水历史信息")
    @PostMapping("/updateHis")
    public CommonResult<History> updateHistory(
            @ApiParam(name = "history", value = "前端采集送水历史信息", required = true)
            @RequestBody History history,
            @ApiParam(name = "workerId", value = "前端采集的送水工id", required = true)
            @RequestParam Integer workerId,
            @ApiParam(name = "custId", value = "前端采集的客户id", required = true)
            @RequestParam Integer custId) {
        log.info(" update custId id = " + custId);
        log.info(" update worker id = " + workerId);
        log.info(" update History = " + history);
        Worker worker = new Worker();
        worker.setWid(workerId);

        Customer customer = new Customer();
        customer.setCid(custId);
        history.setCustomer(customer);
        history.setWorker(worker);
        // 修改送水历史，返回受影响行数
        int rows = historyService.updateHistory(history);
        log.info("update history rows = " + rows);
        if (rows > 0) {
            return new CommonResult<>(ResponseCode.RESPONSE_SUCCESS, "ok", history);
        } else {
            return new CommonResult<>(ResponseCode.RESPONSE_FAIL, "fail", history);
        }
    }

    @ApiOperation("根据时间区间查询送水历史信息")
    @GetMapping("/searchHistory/{pageNum}/{pageSize}/{beginDate}/{endDate}")
    public CommonResult<PageInfo> searchHistory(
            @ApiParam(name="pageNum",value="当前页",required = true)
            @PathVariable("pageNum") Integer pageNum,
            @ApiParam(name = "pageSize", value = "每页数据量", required = true)
            @PathVariable("pageSize") Integer pageSize,
            @ApiParam(name = "beginDate", value = "搜索条件", required = true)
            @PathVariable("beginDate") String beginDate,
            @ApiParam(name = "endDate", value = "搜索条件")
            @PathVariable("endDate") String endDate) {
        log.info("searchHistory beginDate = " + beginDate + " endDate = " + endDate);
        PageInfo<History> historyPageInfo = historyService.searchHistoryByCondition(pageNum, pageSize, beginDate, endDate);
        return new CommonResult<>(ResponseCode.RESPONSE_SUCCESS, "ok", historyPageInfo);
    }

    @ApiOperation("批量删除")
    @PostMapping("/batchDelete")
    public CommonResult<Integer> batchDeleteHistory(
            @ApiParam(name = "string", value = "id集合", required = true)
            @RequestParam String idList) {
        log.info("batchDeleteHistory idList = " + idList);
        int rows = historyService.batchDeleteHistory(idList);
        if (rows > 0) {
            return new CommonResult<>(ResponseCode.RESPONSE_SUCCESS, "ok", rows);
        } else {
            return new CommonResult<>(ResponseCode.RESPONSE_FAIL, "fail", rows);
        }
    }


}
